﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace geopg_wp7
{
    public partial class MapPage : PhoneApplicationPage
    {
        public MapPage()
        {
            InitializeComponent();
        }

        private GeoCoordinate mapCenter;
        double lati;
        double longi;
        Pushpin pin1;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (this.NavigationContext.QueryString.ContainsKey("latitude"))
                lati = double.Parse(this.NavigationContext.QueryString["latitude"]);
            if (this.NavigationContext.QueryString.ContainsKey("longitude"))
                longi = double.Parse(this.NavigationContext.QueryString["longitude"]);
            Microsoft.Phone.Controls.Maps.Platform.Location location =
new Microsoft.Phone.Controls.Maps.Platform.Location();
            location.Latitude = lati;
            location.Longitude = longi;
            MessageBox.Show(location.Latitude.ToString() + " - " + location.Longitude.ToString());
            map1.SetView(location, 16);
        }

        void pinButton_Click(object sender, RoutedEventArgs e)
        {
            // Get center of current map-view
            mapCenter = map1.Center;
            // Create pushpin
            pin1 = new Pushpin();
            pin1.Location = mapCenter;
            pin1.Content = pin1.Location.ToString();
            map1.Children.Add(pin1);
            pin1.MouseLeftButtonUp += new MouseButtonEventHandler(pin1_MouseLeftButtonUp);
            map1.SetView(pin1.Location, 16);
        }

        void pin1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // TO DO: show information (address, etc.) for push-pinned location
        }

        void doneButton_Click(object sender, RoutedEventArgs e)
        {
            GlobalVariables.latitude = pin1.Location.Latitude.ToString();
            GlobalVariables.longitude = pin1.Location.Longitude.ToString();
            NavigationService.Navigate(new Uri("//UploadPage.xaml", UriKind.Relative));
        }

        void zoomIn_Click(object sender, EventArgs e)
        {
            double zoom;
            zoom = map1.ZoomLevel;
            map1.ZoomLevel = ++zoom;
        }

        void zoomOut_Click(object sender, EventArgs e)
        {
            double zoom;
            zoom = map1.ZoomLevel;
            map1.ZoomLevel = --zoom;
        }

        private void MapStyle_Click(object sender, RoutedEventArgs e)
        {
            if (map1.Mode is AerialMode)
                map1.Mode = new RoadMode();
            else
                map1.Mode = new AerialMode();
        }
    }
}